package com.lebang.service.cences;

import com.lebang.domain.CencesHundredDownloadRecord;
import com.lebang.domain.DownloadMemberRecord;
import com.lebang.example.CencesHundredDownloadRecordExample;
import com.lebang.mapper.CencesHundredDownloadRecordMapper;
import com.lebang.mapper.DownloadMemberRecordMapper;
import com.lebang.mapper.LbbMemberMapper;
import com.lebang.model.cences.DoubleConfig;
import com.lebang.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * @Author ljx
 * @Date 2020-12-03
 */
@Service
public class CencesHundredDownloadRecordService extends BaseService<CencesHundredDownloadRecord, Integer, CencesHundredDownloadRecordExample> {
    @Autowired
    private LbbMemberMapper lbbMemberMapper;
    @Autowired
    private DownloadMemberRecordMapper downloadMemberRecordMapper;
    @Autowired
    private CencesHundredDownloadRecordMapper mapper;

    public void
    starDataInit(LocalDateTime end, List<DoubleConfig> configList) {
        this.starDataInit(end, configList, null);
    }

    /**
     * 指定日期公交每日必赚统计
     *
     * @param end
     * @param brand
     */
    public void starDataInit(LocalDateTime end, List<DoubleConfig> configList, List<String> brand) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMdd");
        LocalDateTime star = end.minusDays(1);

        //统计日期
        int time = Integer.parseInt(end.toLocalDate().minusDays(1).format(df));

        if (brand == null) {
            //所有渠道
            brand = lbbMemberMapper.selectBrand();
        }

        //展示数
        List<CencesHundredDownloadRecord> appStoreShowCount = downloadMemberRecordMapper.selectShowCount(star, end);
        Map<String, List<CencesHundredDownloadRecord>> showCountMap = appStoreShowCount.stream().collect(Collectors.groupingBy(CencesHundredDownloadRecord::getAppStore));

        //完成数
        List<CencesHundredDownloadRecord> appStoreSuccessCount = downloadMemberRecordMapper.getSuccessCount(star, end);
        Map<String, List<CencesHundredDownloadRecord>> successCountMap = appStoreSuccessCount.stream().collect(Collectors.groupingBy(CencesHundredDownloadRecord::getAppStore));

        //完成人数
        List<CencesHundredDownloadRecord> appStoreSuccessMemberCount = downloadMemberRecordMapper.selectSuccessMemberCount(star, end);
        Map<String, List<CencesHundredDownloadRecord>> successMemberCountMap = appStoreSuccessMemberCount.stream().collect(Collectors.groupingBy(CencesHundredDownloadRecord::getAppStore));

        //放弃数
        List<CencesHundredDownloadRecord> appStoreFailCount = downloadMemberRecordMapper.selectFailCount(star, end);
        Map<String, List<CencesHundredDownloadRecord>> failCountMap = appStoreFailCount.stream().collect(Collectors.groupingBy(CencesHundredDownloadRecord::getAppStore));

        //待提交数
        List<CencesHundredDownloadRecord> appStoreWaitCount = downloadMemberRecordMapper.selectWaitCount(star, end);
        Map<String, List<CencesHundredDownloadRecord>> waitCountMap = appStoreWaitCount.stream().collect(Collectors.groupingBy(CencesHundredDownloadRecord::getAppStore));

        //查询今天每日记录
        List<DownloadMemberRecord> recordList = downloadMemberRecordMapper.selectTodayRecord(star, end);
        //档位
        for (DownloadMemberRecord record : recordList) {
            for (DoubleConfig config : configList) {
                if (config.getType().equals(record.getHasFirst())) {
                    if (config.getTime() <= record.getTime()) {
                        record.setGears(config.getCountName());
                    }
                }
            }
        }
        Map<String, List<DownloadMemberRecord>> gearsMap = recordList.stream().collect(Collectors.groupingBy(DownloadMemberRecord::getAppStore));

        AtomicInteger allShowCount = new AtomicInteger(0);
        AtomicInteger allSuccessCount = new AtomicInteger(0);
        AtomicInteger allSuccessMemberCount = new AtomicInteger(0);
        AtomicInteger allFailCount = new AtomicInteger(0);
        AtomicInteger allWaitCount = new AtomicInteger(0);
        AtomicInteger allOneGearsCount = new AtomicInteger(0);
        AtomicInteger allTwoGearsCount = new AtomicInteger(0);
        AtomicInteger allThreeGearsCount = new AtomicInteger(0);
        AtomicInteger allFourGearsCount = new AtomicInteger(0);

        //分渠道
        for (String s : brand) {
            BigDecimal brandShowCount = new BigDecimal(this.getShowCount(showCountMap, s));
            BigDecimal brandSuccessCount = new BigDecimal(this.getSuccessCount(successCountMap, s));
            BigDecimal brandSuccessMemberCount = new BigDecimal(this.getSuccessMemberCount(successMemberCountMap, s));

            CencesHundredDownloadRecord record = new CencesHundredDownloadRecord();
            record.setCreateTime(LocalDateTime.now());
            record.setTime(time);
            record.setHasDeleted(0);
            record.setAppStore(s);
            record.setShowCount(brandShowCount.intValue());
            record.setSuccessCount(brandSuccessCount.intValue());
            record.setSuccessMemberCount(brandSuccessMemberCount.intValue());
            //完成比例
            if (brandShowCount.intValue() == 0){
                record.setSuccessScale(0);
            }else {
                record.setSuccessScale(brandSuccessCount.divide(brandShowCount, 2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).intValue());
            }
            //人均完成数
            if (brandSuccessMemberCount.intValue() == 0){
                record.setAvgSuccessCount(0);
            }else {
                record.setAvgSuccessCount(brandSuccessCount.divide(brandSuccessMemberCount, 2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).intValue());
            }

            record.setFailCount(this.getFailCount(failCountMap, s));
            record.setWaitCount(this.getWaitCount(waitCountMap, s));
            //1档人数
            record.setOneGearsCount(this.getGearsCount(gearsMap, 1, s));
            //2档人数
            record.setTwoGearsCount(this.getGearsCount(gearsMap, 2, s));
            //3档人数
            record.setThreeGearsCount(this.getGearsCount(gearsMap, 3, s));
            //4档人数
            record.setFourGearsCount(this.getGearsCount(gearsMap, 4, s));

            mapper.insertSelective(record);
            allShowCount.addAndGet(record.getShowCount());
            allSuccessCount.addAndGet(record.getSuccessCount());
            allSuccessMemberCount.addAndGet(record.getSuccessMemberCount());
            allFailCount.addAndGet(record.getFailCount());
            allWaitCount.addAndGet(record.getWaitCount());
            allOneGearsCount.addAndGet(record.getOneGearsCount());
            allTwoGearsCount.addAndGet(record.getTwoGearsCount());
            allThreeGearsCount.addAndGet(record.getThreeGearsCount());
            allFourGearsCount.addAndGet(record.getFourGearsCount());

        }
        BigDecimal show=new BigDecimal(allShowCount.get());
        BigDecimal success=new BigDecimal(allSuccessCount.get());
        BigDecimal successMember=new BigDecimal(allSuccessMemberCount.get());
        //所有渠道
        CencesHundredDownloadRecord record = new CencesHundredDownloadRecord();
        record.setCreateTime(LocalDateTime.now());
        record.setHasDeleted(0);
        record.setTime(time);
        record.setAppStore("all");
        record.setShowCount(show.intValue());
        record.setSuccessCount(success.intValue());
        record.setSuccessMemberCount(successMember.intValue());
        if (show.intValue()==0){
            record.setSuccessScale(0);
        }else {
            record.setSuccessScale(success.divide(show,2,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).intValue());
        }
        if (successMember.intValue()==0){
            record.setAvgSuccessCount(0);
        }else {
            record.setAvgSuccessCount(success.divide(successMember,2,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).intValue());
        }
        record.setFailCount(allFailCount.get());
        record.setWaitCount(allWaitCount.get());
        record.setOneGearsCount(allOneGearsCount.get());
        record.setTwoGearsCount(allTwoGearsCount.get());
        record.setThreeGearsCount(allThreeGearsCount.get());
        record.setFourGearsCount(allFourGearsCount.get());

        mapper.insertSelective(record);
    }


    /**
     * 展示数
     *
     * @param map
     * @param appStore
     * @return
     */
    private Integer getShowCount(Map<String, List<CencesHundredDownloadRecord>> map, String appStore) {
        AtomicInteger finish = new AtomicInteger(0);
        List<CencesHundredDownloadRecord> list = map.get(appStore);
        if (list == null) {
            return finish.get();
        }
        list.forEach(s -> {
            finish.addAndGet(s.getShowCount());
        });
        return finish.get();
    }

    /**
     * 完成数
     *
     * @param map
     * @param appStore
     * @return
     */
    private Integer getSuccessCount(Map<String, List<CencesHundredDownloadRecord>> map, String appStore) {
        AtomicInteger finish = new AtomicInteger(0);
        List<CencesHundredDownloadRecord> list = map.get(appStore);
        if (list == null) {
            return finish.get();
        }
        list.forEach(s -> {
            finish.addAndGet(s.getSuccessCount());
        });
        return finish.get();
    }

    /**
     * 完成人数
     *
     * @param map
     * @param appStore
     * @return
     */
    private Integer getSuccessMemberCount(Map<String, List<CencesHundredDownloadRecord>> map, String appStore) {
        AtomicInteger finish = new AtomicInteger(0);
        List<CencesHundredDownloadRecord> list = map.get(appStore);
        if (list == null) {
            return finish.get();
        }
        list.forEach(s -> {
            finish.addAndGet(s.getSuccessMemberCount());
        });
        return finish.get();
    }

    /**
     * 放弃数
     *
     * @param map
     * @param appStore
     * @return
     */
    private Integer getFailCount(Map<String, List<CencesHundredDownloadRecord>> map, String appStore) {
        AtomicInteger finish = new AtomicInteger(0);
        List<CencesHundredDownloadRecord> list = map.get(appStore);
        if (list == null) {
            return finish.get();
        }
        list.forEach(s -> {
            finish.addAndGet(s.getFailCount());
        });
        return finish.get();
    }

    /**
     * 待提交数
     *
     * @param map
     * @param appStore
     * @return
     */
    private Integer getWaitCount(Map<String, List<CencesHundredDownloadRecord>> map, String appStore) {
        AtomicInteger finish = new AtomicInteger(0);
        List<CencesHundredDownloadRecord> list = map.get(appStore);
        if (list == null) {
            return finish.get();
        }
        list.forEach(s -> {
            finish.addAndGet(s.getWaitCount());
        });
        return finish.get();
    }

    /**
     * 档位数
     *
     * @param map
     * @param type
     * @param appStore
     * @return
     */
    private Integer getGearsCount(Map<String, List<DownloadMemberRecord>> map, Integer type, String appStore) {
        AtomicInteger finish = new AtomicInteger(0);
        List<DownloadMemberRecord> list = map.get(appStore);
        if (list == null) {
            return finish.get();
        }
        list.forEach(s -> {
            if (type.equals(s.getGears())) {
                finish.addAndGet(1);
            }
        });
        return finish.get();
    }

    public int deleteRecord(Integer time) {
        CencesHundredDownloadRecordExample example=new CencesHundredDownloadRecordExample();
        example.createCriteria().andTimeEqualTo(time);
        return mapper.deleteByExample(example);
    }
}
